home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / b / b.lha / B / src / bint / b1mem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-11-24  |  3.0 KB  |  165 lines

  1. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  2.  
  3. /*
  4.   $Header: b1mem.c,v 1.4 85/08/27 10:55:51 timo Exp $
  5. */
  6.  
  7. /* B memory management */
  8.  
  9. #include "b.h"
  10. #include "b1obj.h"
  11. #include "b1mem.h"
  12. #include "b3err.h" /* For still_ok */
  13.  
  14. #ifdef IBMPC
  15. #define RESERVE
  16. #endif
  17.  
  18. #ifdef RESERVE
  19. Forward char *mymalloc(), *myrealloc();
  20. #else
  21. #define mymalloc malloc
  22. #define myrealloc realloc
  23. #define initreserve() 0 /* Dummy routine */
  24. #endif
  25.  
  26. #ifdef ebug
  27.  
  28. Hidden value notel, inotel;
  29. Visible bool noting= Yes;
  30.  
  31. Hidden Procedure note(p) int p; {
  32.     if (!noting) {
  33.         value ip;
  34.         noting= Yes;
  35.         insert(ip= mk_integer(p), ¬el);
  36.         release(ip);
  37.         noting= No;
  38.     }
  39. }
  40.  
  41. Hidden Procedure denote(p) int p; {
  42.     if (!noting) {
  43.         value ip;
  44.         noting= Yes;
  45.         if (in(ip= mk_integer(p), notel)) {
  46.             remove(ip, ¬el);
  47.             if (inotel != Vnil && in(ip, inotel))
  48.                 remove(ip, &inotel);
  49.         }
  50. #ifndef IBMPC
  51.         else syserr(MESS(600, "releasing illegally"));
  52. #endif
  53.         release(ip);
  54.         noting= No;
  55.     }
  56. }
  57.  
  58. Visible Procedure initmem() {
  59.     notel= mk_elt(); noting= No;
  60.     initreserve();
  61. }
  62.  
  63. Visible Procedure end_init() {
  64.     noting= Yes;
  65.     inotel= copy(notel);
  66.     noting= No;
  67. }
  68.  
  69. Visible Procedure term_mem() {
  70.     int l, i; value v;
  71.     testing= noting= Yes;
  72.     l= length(notel);
  73.     if (l>length(inotel)) fprintf(stdout, "Unreleased:\n");
  74.     for(i=1; i<=l; i++) {
  75.         v= thof(i, notel);
  76.         if (!in(v, inotel))
  77.             { wri((value) intval(v), No, No, No); newline(); }
  78.     }
  79. }
  80.  
  81. Visible Procedure endmem() {
  82.     release(inotel); inotel= Vnil;
  83.     release(notel); notel= Vnil;
  84. }
  85.  
  86. #else !ebug
  87.  
  88. #define note(p)
  89. #define denote(p)
  90.  
  91. Visible Procedure initmem()
  92. {
  93.     initreserve();
  94. }
  95.  
  96. Visible Procedure end_init() {}
  97. Visible Procedure term_mem() {}
  98. Visible Procedure endmem() {}
  99.  
  100. #endif ebug
  101.  
  102. #define Negative MESS(601, "creating value of exceedingly large size")
  103.  
  104. Visible ptr getmem(syze) unsigned syze; {
  105.     ptr p;
  106.     if ((int) (syze) < 0) error(Negative);
  107.     p= (ptr) mymalloc(syze);
  108.     note(p);
  109.     if (bugs || p==Nil) printf("{%u}",syze);
  110.     if (p == Nil) memexh();
  111.     return p;
  112. }
  113.  
  114. Visible Procedure regetmem(v, syze) ptr *v; unsigned syze; {
  115.     if ((int) (syze) < 0) error(Negative);
  116.     if (bugs) printf("[%u]",syze);
  117.     denote(*v);
  118.     *v= myrealloc(*v, syze);
  119.     note(*v);
  120.     if (*v == Nil) memexh();
  121. }
  122.  
  123. Visible Procedure freemem(p) ptr p; {
  124.     denote(p);
  125.     free(p);
  126. }
  127.  
  128. #ifdef RESERVE
  129.  
  130. #define RES_SIZE 3000
  131.  
  132. Hidden char *p_reserve= Nil;
  133.  
  134. Hidden Procedure initreserve() {
  135.     p_reserve= malloc(RES_SIZE);
  136. }
  137.  
  138. Hidden char *mymalloc(syze) unsigned syze; {
  139.     char *p;
  140.     p= malloc(syze);
  141.     if (p != Nil && p_reserve != Nil) return p;
  142.     if (p_reserve != Nil) {
  143.         free(p_reserve); p_reserve= Nil;
  144.         p= malloc(syze);
  145.         if (p != Nil) error(MESS(602, "out of memory"));
  146.     }
  147.     if (p_reserve == Nil) initreserve();
  148.     return p;
  149. }
  150.  
  151. Hidden char *myrealloc(p1, syze) char *p1; unsigned syze; {
  152.     char *p;
  153.     p= realloc(p1, syze);
  154.     if (p != Nil && p_reserve != Nil) return p;
  155.     if (p_reserve != Nil) {
  156.         free(p_reserve); p_reserve= Nil;
  157.         p= realloc(p1, syze);
  158.         if (p != Nil) error(MESS(602, "out of memory"));
  159.     }
  160.     if (p_reserve == Nil) initreserve();
  161.     return p;
  162. }
  163.  
  164. #endif RESERVE
  165.